const target = {
    foo:'bar'
};

const handler = {
    /**
     * 
     * @param {*} trapTarget 目标对象
     * @param {*} property 属性
     * @param {*} receiver 代理对象
     */
    get(trapTarget,property,receiver){
        console.log(trapTarget === target);
        console.log(property);
        console.log(receiver === proxy);
    }
}

const proxy = new Proxy(target,handler);

proxy.foo;
//true
//foo
//true


/**
 * 捕获后修改
 */
{
    const target = {
        foo:'bar2'
    };
    const handler = {
        get(trapTarget,property){
            return '拦截后：'+trapTarget[property]
        }
    }
    const proxy = new Proxy(target,handler);

    console.log(proxy.foo);//拦截后：bar2
}

console.log(target.foo);//bar


/**
 * 反射API（reflect）
 */
{
    const target = {
        foo:'bar3'
    };
    const handler = {
        get(){
            return Reflect.get(...arguments);
        }
    }
    const proxy = new Proxy(target,handler);
    console.log(proxy.foo);//bar3
    console.log(target.foo);//bar3
}
//简洁版
{
    const target = {
        foo:'bar4'
    };
    const handler = {
        get:Reflect.get
    }
    const proxy = new Proxy(target,handler);
    console.log(proxy.foo);//bar4
    console.log(target.foo);//bar4 
}
//转发所有反射
{
    const target = {
        foo:'bar5'
    };
    const proxy = new Proxy(target,Reflect);
    console.log(proxy.foo);//bar5
    console.log(target.foo);//bar5 
}

/**
 * 捕获后修改2
 */
{
    const target = {
        foo:'bar6',
        baz:'qux'
    };
    const handler = {
        get(trapTarget,property,receiver){
            let string = ''
            if (property === 'foo') {
                string = '!!!'
            }
            return trapTarget[property] + string
        }
    }
    const proxy = new Proxy(target,handler);
    console.log(proxy.foo);//bar6!!!
    console.log(target.baz);//qux
}
